{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Day15 re中的sub"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "　　在今天的日程里，我们将继续对`re`库中常用的一些API进行学习，今天要学习的是`re`中的`sub`，`sub`代表单词*substitute*，即替换，我们通过`re.sub`可以用正则表达式来概括字符串中我们想要替换的部分。\n",
    "\n",
    "　　`sub`的第一个参数`pattern`代表将要替换的字符串片段的正则模式，第二个参数`repl`代表替换之后新的字符串，而第三个参数`string`代表原始字符串，第四个参数`count`代表代表最大替换次数，默认为0时代表不限制，而最后一个参数`flags`之前的日程中已经介绍过，这里不再赘述。\n",
    "  \n",
    "　　首先从1个简单的例子出发："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2020-09-14T13:30:28.321367Z",
     "iopub.status.busy": "2020-09-14T13:30:28.321367Z",
     "iopub.status.idle": "2020-09-14T13:30:28.326354Z",
     "shell.execute_reply": "2020-09-14T13:30:28.326354Z",
     "shell.execute_reply.started": "2020-09-14T13:30:28.321367Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'23.12'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "# 将所有非数字和小数点的部分替换为空字符\n",
    "s = '“23.12%”'\n",
    "\n",
    "re.sub('[^\\d\\.]+', '', s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "　　可以看到，通过`sub`，我们传入的正则模式即为我们要搜索并进行空字符替换的字符片段，这在将原本脏乱的数值清洗为纯数值时非常实用。\n",
    "\n",
    "　　除了`sub`，`re`中还有一个功能相似的API：`subn`，不同的是它在对原始字符串进行替换的同时，会一并返回总共发生替换的次数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2020-09-14T13:30:28.327353Z",
     "iopub.status.busy": "2020-09-14T13:30:28.327353Z",
     "iopub.status.idle": "2020-09-14T13:30:28.333334Z",
     "shell.execute_reply": "2020-09-14T13:30:28.333334Z",
     "shell.execute_reply.started": "2020-09-14T13:30:28.327353Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('汪文斌指出，美国个别政客一方面鼓吹要实现公平对等，构建所谓的“清洁网络”，另一方面却又在拿不出任何证据的情况下，泛化国家安全概念，滥用国家力量，对某一领域取得领先优势的非美国企业进行无理打压和百般胁迫，这充分暴露了美方少数政客强取豪夺的真实用意和经济霸凌的丑陋面目。',\n",
       " 3)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将所有换行符替换为空，并返回总共找到的换行次数\n",
    "s = '''汪文斌指出，美国个别政客一方面鼓吹要实现公平对等，构建所谓的“清洁网络”，\n",
    "另一方面却又在拿不出任何证据的情况下，泛化国家安全概念，滥用国家力量，对某一领域\n",
    "取得领先优势的非美国企业进行无理打压和百般胁迫，这充分暴露了美方少数政客强取豪夺\n",
    "的真实用意和经济霸凌的丑陋面目。'''\n",
    "\n",
    "re.subn('\\n+', '', s)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Day15 课后小测验\n",
    "\n",
    "　　`sub`在数据清洗场景中使用非常高频，在学过今天的内容之后，请你利用`sub`，将下列列表中所有的元素转化为可执行运算的数值，并计算出他们的**最大值**，其中K代表千，M代表百万："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2020-09-14T13:30:28.335331Z",
     "iopub.status.busy": "2020-09-14T13:30:28.334332Z",
     "iopub.status.idle": "2020-09-14T13:30:28.339319Z",
     "shell.execute_reply": "2020-09-14T13:30:28.338321Z",
     "shell.execute_reply.started": "2020-09-14T13:30:28.335331Z"
    }
   },
   "outputs": [],
   "source": [
    "target = ['32132.321', '12.89%', '20K', '120M', '200,000,000']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2020-08-26T12:55:19.898241Z",
     "iopub.status.busy": "2020-08-26T12:55:19.897245Z",
     "iopub.status.idle": "2020-08-26T12:55:19.904225Z",
     "shell.execute_reply": "2020-08-26T12:55:19.903227Z",
     "shell.execute_reply.started": "2020-08-26T12:55:19.898241Z"
    }
   },
   "source": [
    "　　请将你的答案截图发到本帖评论区~"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200000000"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "max([eval(re.sub('%','/100',re.sub('K','*1000',re.sub('M','*1000000',re.sub(',','',_))))) for _ in target])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
